<template>
  <!-- 资讯分类管理 -->
  <div>
    <el-button type="primary" @click="toAdd">添加</el-button>
    <el-table :data="categoryData" tooltip-effect="dark" style="width: 100%" :v-loading="loading">
      <el-table-column type="index" label="序号" :index="1" width="180" />
      <el-table-column prop="no" label="咨询编号" width="180" />
      <el-table-column prop="id" label="栏目id" width="180" />
      <el-table-column prop="name" label="栏目名称" width="180" />
      <el-table-column prop="description" label="栏目描述" width="180" />
      <el-table-column prop="parentId" label="父栏目id" width="180" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" @click.prevent="toEdit(scope.row)">编辑</a>
          <a href="#" @click.prevent="toDelete(scope.row)">删除</a>
        </template>
      </el-table-column>
    </el-table>
    <!-- 保存或更新模态框 -->
    <el-dialog :title="title" :visible.sync="visible">
      <!-- close表示模态框关闭时触发的回调 -->
      <el-form ref="myForm" :rules="rules" :model="form" @close="toClose">
        <el-form-item label="栏目id" :label-width="formLabelWidth" prop="id">
          <el-input v-model="form.id" autocomplete="off" />
        </el-form-item>
        <el-form-item label="栏目名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item label="栏目描述" :label-width="formLabelWidth" prop="description">
          <el-input v-model="form.description" autocomplete="off" />
        </el-form-item>
        <el-form-item label="序号" :label-width="formLabelWidth" prop="no">
          <el-input v-model="form.no" autocomplete="off" />
        </el-form-item>
        <el-form-item label="父栏目id" :label-width="formLabelWidth" prop="parentId">
          <el-input v-model="form.parentId" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryCateConsult, deleteConsult, saveOrUpdate } from '@/api/infoCategory'

export default {
  data() {
    return {
      // 资讯栏目数据
      categoryData: [],
      params: {},
      loading: false,
      // 模态框状态
      visible: false,
      // 表单对象
      form: {},
      formLabelWidth: '120px',
      rules: {
        name: [{ required: true, message: '请输入栏目名称', trigger: 'blur' }]
      },
      // 批量选中
      multipleSelection: [],
      title: ''
    }
  },
  computed: {
  },
  created() {
    this.getConsultColumn()
  },
  mounted() {},
  methods: {
    // 查询所有资讯栏目
    async getConsultColumn() {
      this.loading = true
      for (const key in this.params) {
        if (!this.params[key]) {
          delete this.params[key]
        }
      }
      const res = await queryCateConsult(this.params)
      this.categoryData = res.data
      this.loading = false
    },
    // 根据id删除资讯栏目
    async toDelete(row) {
      await deleteConsult(row)
      this.getConsultColumn()
    },
    // 添加
    toAdd() {
      this.title = '新增咨询栏目'
      this.visible = true
      this.form = {}
    },
    // 编辑
    toEdit(row) {
      this.title = '编辑咨询信息'
      this.visible = true
      this.form = { ...row }
    },
    // 模态框关闭回调
    toClose() {
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 500)
    },
    // 保存更新
    toSubmit() {
      // 表单校验
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.getConsultColumn()
          this.toClose()
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
</style>
